<template>
  <div class="container-login">
    <div class="login-title">SIN</div>
    <div class="login-box">
      <div class="login-img">
        <div class="trans trans-one"></div>
        <div class="trans trans-two"></div>
        <div class="trans trans-three"></div>
        <div class="trans trans-four"></div>
        <div class="user-img"></div>
      </div>
      <div class="login-txt">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
          <el-form-item prop="username">
            <el-input @keyup.enter.native="handleLogin" class="login-msg" v-model="loginForm.username" prefix-icon="el-icon-s-custom" placeholder="请输入用户名"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input @keyup.enter.native="handleLogin" class="login-msg" v-model="loginForm.password" prefix-icon="el-icon-lock" placeholder="请输入密码" show-password></el-input>
          </el-form-item>
          <el-checkbox v-model="loginForm.rememberMe" style="margin: 0 0 25px 0; color: #eee">记住密码</el-checkbox>
          <el-form-item style="width: 100%">
            <div class="btn-login" @click="handleLogin">
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </div>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
// import { getCodeImg } from "@/api/login";
import Cookies from 'js-cookie'
import {encrypt, decrypt} from '@/utils/jsencrypt'

export default {
  name: 'Login',
  data() {
    return {
      codeUrl: '',
      cookiePassword: '',
      loginForm: {
        username: 'admin',
        password: 'admin123',
        rememberMe: false,
        code: '',
        uuid: ''
      },
      loginRules: {
        username: [{required: true, trigger: 'blur', message: '用户名不能为空'}],
        password: [{required: true, trigger: 'blur', message: '密码不能为空'}]
        // code: [{ required: true, trigger: "change", message: "验证码不能为空" }]
      },
      loading: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    // this.getCode();
    this.getCookie()
  },
  methods: {
    // getCode() {
    //   getCodeImg().then(res => {
    //     this.codeUrl = "data:image/gif;base64," + res.img;
    //     this.loginForm.uuid = res.uuid;
    //   });
    // },
    getCookie() {
      const username = Cookies.get('username')
      const password = Cookies.get('password')
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          if (this.loginForm.rememberMe) {
            Cookies.set('username', this.loginForm.username, {expires: 30})
            Cookies.set('password', encrypt(this.loginForm.password), {expires: 30})
            Cookies.set('rememberMe', this.loginForm.rememberMe, {expires: 30})
          } else {
            Cookies.remove('username')
            Cookies.remove('password')
            Cookies.remove('rememberMe')
          }
          this.login(this.loginForm, false)
        }
      })
    },
    // 登录
    login(user, force) {
      user.force = force
      this.$store
        .dispatch('Login', user)
        .then(res => {
          if (res.code === 200) {
            this.$router.push({path: this.redirect || '/'}, () => {})
          } else if (res.code === 203) {
            this.$confirm(`该账号已在${res.msg}登录，是否强制登录？`, '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            })
              .then(() => {
                this.login(user, true)
              })
              .catch(() => {
                location.reload() // 为了重新实例化vue-router对象 避免bug
              })
          }
        })
        .catch(() => {
          this.loading = false
          // this.getCode();
        })
    }
  }
}
</script>

<style scoped>
.container-login {
  width: 100%;
  height: 100%;
  background-image: url(../assets/images/login/login.png);
  background-size: 100% 100%;
}

.login-title {
  color: #ffffff;
  position: absolute;
  width: 800px;
  height: 70px;
  /*background-image: url(../assets/images/login/title.png);*/
  /*background-size: 100% 100%;*/
  top: calc(50% - 270px);
  left: calc(50% - 400px);
  line-height: 70px;
  font-weight: bolder;
  font-size: 42px;
  letter-spacing: 10px;
  text-indent: 50px;
  text-align: center;
  background-image: -webkit-linear-gradient(bottom, #e7ecf0, #a7e5e9, #1890ff);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}
.login-box {
  position: absolute;
  top: calc(50% - 188px);
  left: calc(50% - 315px);
  width: 631px;
  height: 377px;
  background: url(../assets/images/login/login_box.png) -3px -6px no-repeat;
  background-size: 100% 100%;
  border-radius: 5px;
  z-index: 99;
}

.login-txt {
  position: absolute;
  width: 280px;
  left: 45%;
  top: 90px;
}

.login-txt > .login-msg {
  margin-top: 20px;
}

.trans {
  width: 100%;
  height: 100%;
  position: absolute;
  left: 0;
  top: 0;
}

@keyframes blurOne {
  from {
    transform: rotateZ(10deg);
  }
  to {
    transform: rotateZ(350deg);
  }
}

.trans-one {
  animation: blurOne 8s linear 0s infinite alternate;
  background: url('../assets/images/login/blue_one.png') 34px 35px no-repeat;
  border-radius: 100px;
  z-index: 5;
}

@keyframes blurTwo {
  from {
    transform: rotateZ(360deg);
  }
  to {
    transform: rotateZ(20deg);
  }
}

.trans-two {
  animation: blurTwo 8s linear 0s infinite alternate;
  background: url('../assets/images/login/blue_two.png') 30px 29px no-repeat;
  z-index: 4;
}

@keyframes blurThree {
  from {
    transform: rotateZ(10deg);
  }
  to {
    transform: rotateZ(360deg);
  }
}

.trans-three {
  animation: blurThree 8s linear 0s infinite alternate;
  background: url('../assets/images/login/blue_three.png') 13px 13px no-repeat;
  z-index: 3;
}

@keyframes blurFour {
  from {
    transform: rotateZ(360deg);
  }
  to {
    transform: rotateZ(10deg);
  }
}

.trans-four {
  animation: blurFour 8s linear 0s infinite alternate;
  background: url('../assets/images/login/blue_four.png') 84px 5px no-repeat, url('../assets/images/login/blue_five.png') 5px 104px no-repeat;
  border-radius: 100px;
  z-index: 2;
}

.login-img {
  width: 200px;
  height: 200px;
  position: absolute;
  left: 70px;
  top: 88px;
}

.user-img {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-size: 80% 80%;
  background: url('../assets/images/login/login_user.png') no-repeat center center;
}
.btn-login {
  width: 100%;
  height: 36px;
  text-align: center;
  line-height: 36px;
  color: #ffffff;
  background-image: url(../assets/images/login/button.png);
  background-size: 100% 100%;
  cursor: pointer;
  transition: all 0.2s;
}
.btn-login:hover {
  filter: brightness(125%);
}
/deep/.el-input__inner {
  background-color: rgba(0, 0, 0, 0.3) !important;
  border: none !important;
  color: #7594bf !important;
}
/deep/.el-checkbox__inner {
  background-color: rgba(0, 0, 0, 0.3) !important;
}
/deep/.el-checkbox__label {
  color: #7594bf;
}
</style>
